package br.com.fafica.fast_restaurant.view;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JSeparator;
import javax.swing.JList;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Vector;

import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import br.com.fafica.fast_restaurant.control.ControlMesa;
import br.com.fafica.fast_restaurant.model.dao.FabricaConecxao;

public class TelaPesquisarMesa extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	private JTable table;

	public void run() {
		try {
			TelaPesquisarMesa frame = new TelaPesquisarMesa();
			frame.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public TelaPesquisarMesa() {
		setTitle("PESQUISA MESA");
		final TelaPesquisarMesa estaTela = this;
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 800, 600);
		contentPane = new JPanel();
		contentPane.setBackground(new Color(255, 255, 255));
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);

		JPanel panel = new JPanel();
		panel.setBackground(new Color(0, 100, 0));

		JLabel label = new JLabel();
		label.setText("C\u00D3DIGO DA MESA:");
		label.setForeground(new Color(255, 215, 0));
		label.setFont(new Font("Tahoma", Font.BOLD, 12));
		label.setBackground(Color.GRAY);

		textField = new JTextField();
		textField.setColumns(10);

		JButton button = new JButton("PESQUISAR");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {

				ArrayList<String> Parametros = new ArrayList<String>();
				Parametros.add(textField.getText().toUpperCase());

				Vector<String> vColunas = new Vector<String>();
				vColunas = PegaColunaDaGrade();

				DefaultTableModel objTbela = new DefaultTableModel(vColunas, 0);

				ControlMesa cntrlMesa = new ControlMesa();

				objTbela = cntrlMesa.Pesquisa(Parametros, objTbela);

				table.setModel(objTbela);

			}

			private Vector<String> PegaColunaDaGrade() {

				Vector<String> colunasTabela = new Vector<String>();
				colunasTabela.add("cod");
				colunasTabela.add("quant_lugar");

				return colunasTabela;
			}

		});

		button.setForeground(new Color(255, 215, 0));
		button.setBackground(new Color(0, 100, 0));
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(gl_panel.createParallelGroup(
				Alignment.LEADING).addGroup(
				gl_panel.createSequentialGroup()
						.addContainerGap()
						.addComponent(label)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(textField, GroupLayout.PREFERRED_SIZE,
								483, GroupLayout.PREFERRED_SIZE)
						.addGap(18)
						.addComponent(button, GroupLayout.PREFERRED_SIZE, 114,
								GroupLayout.PREFERRED_SIZE)
						.addContainerGap(40, Short.MAX_VALUE)));
		gl_panel.setVerticalGroup(gl_panel
				.createParallelGroup(Alignment.LEADING)
				.addGroup(
						gl_panel.createSequentialGroup()
								.addGap(23)
								.addGroup(
										gl_panel.createParallelGroup(
												Alignment.BASELINE)
												.addComponent(
														label,
														GroupLayout.PREFERRED_SIZE,
														15,
														GroupLayout.PREFERRED_SIZE)
												.addComponent(
														textField,
														GroupLayout.PREFERRED_SIZE,
														GroupLayout.DEFAULT_SIZE,
														GroupLayout.PREFERRED_SIZE)
												.addComponent(button))
								.addContainerGap(22, Short.MAX_VALUE)));
		panel.setLayout(gl_panel);

		JSeparator separator = new JSeparator();

		JPanel panel_1 = new JPanel();
		panel_1.setBackground(new Color(0, 100, 0));

		JLabel label_1 = new JLabel();
		label_1.setText("LISTA MESAS:");
		label_1.setForeground(new Color(255, 215, 0));
		label_1.setFont(new Font("Tahoma", Font.BOLD, 12));
		label_1.setBackground(Color.GRAY);

		JScrollPane scrollPane = new JScrollPane();
		GroupLayout gl_panel_1 = new GroupLayout(panel_1);
		gl_panel_1
				.setHorizontalGroup(gl_panel_1
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								gl_panel_1
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												gl_panel_1
														.createParallelGroup(
																Alignment.LEADING)
														.addComponent(
																scrollPane,
																GroupLayout.PREFERRED_SIZE,
																540,
																GroupLayout.PREFERRED_SIZE)
														.addComponent(
																label_1,
																GroupLayout.PREFERRED_SIZE,
																144,
																GroupLayout.PREFERRED_SIZE))
										.addContainerGap(18, Short.MAX_VALUE)));
		gl_panel_1.setVerticalGroup(gl_panel_1.createParallelGroup(
				Alignment.LEADING).addGroup(
				gl_panel_1
						.createSequentialGroup()
						.addContainerGap()
						.addComponent(label_1, GroupLayout.PREFERRED_SIZE, 15,
								GroupLayout.PREFERRED_SIZE)
						.addGap(18)
						.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE,
								188, GroupLayout.PREFERRED_SIZE)
						.addContainerGap(128, Short.MAX_VALUE)));

		table = new JTable();
		table.setModel(new DefaultTableModel(new Object[][] { { null, null },
				{ null, null }, { null, null }, { null, null }, { null, null },
				{ null, null }, { null, null }, { null, null }, { null, null },
				{ null, null }, }, new String[] { "C\u00D3DIGO",
				"QUANTIDADE DE LUGARES" }));
		scrollPane.setViewportView(table);
		panel_1.setLayout(gl_panel_1);

		JSeparator separator_1 = new JSeparator();

		JPanel panel_2 = new JPanel();
		panel_2.setBackground(new Color(255, 255, 255));

		JButton button_2 = new JButton("CANCELAR");
		button_2.setForeground(new Color(255, 215, 0));
		button_2.setBackground(new Color(0, 100, 0));
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				estaTela.setVisible(false);
				TelaPesquisar telaPesquisar = new TelaPesquisar();
				telaPesquisar.setVisible(true);
			}
		});

		JButton button_3 = new JButton("SALVAR");
		button_3.setForeground(new Color(255, 215, 0));
		button_3.setBackground(new Color(0, 100, 0));
		GroupLayout gl_panel_2 = new GroupLayout(panel_2);
		gl_panel_2.setHorizontalGroup(gl_panel_2.createParallelGroup(
				Alignment.TRAILING).addGroup(
				gl_panel_2
						.createSequentialGroup()
						.addContainerGap(502, Short.MAX_VALUE)
						.addComponent(button_3, GroupLayout.PREFERRED_SIZE,
								129, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.UNRELATED)
						.addComponent(button_2, GroupLayout.PREFERRED_SIZE,
								129, GroupLayout.PREFERRED_SIZE).addGap(14)));
		gl_panel_2
				.setVerticalGroup(gl_panel_2
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								gl_panel_2
										.createSequentialGroup()
										.addGroup(
												gl_panel_2
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(
																button_2,
																GroupLayout.PREFERRED_SIZE,
																48,
																GroupLayout.PREFERRED_SIZE)
														.addComponent(
																button_3,
																GroupLayout.PREFERRED_SIZE,
																48,
																GroupLayout.PREFERRED_SIZE))
										.addContainerGap(16, Short.MAX_VALUE)));
		panel_2.setLayout(gl_panel_2);

		JButton button_4 = new JButton("EDITAR");
		button_4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String cod = (String) table.getModel().getValueAt(
						table.getSelectedRow(), 0);
				String quntLugar = (String) table.getModel().getValueAt(
						table.getSelectedRow(), 1);

				TelaEditarMesa tm = new TelaEditarMesa(null, true, cod,
						quntLugar);
				tm.setVisible(true);

			}
		});
		button_4.setForeground(new Color(255, 215, 0));
		button_4.setBackground(new Color(0, 100, 0));

		JButton button_5 = new JButton("EXCLUIR");
		button_5.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				try {
					Connection conn = FabricaConecxao.GeraConexao();
					String query = "delete from cad_mesa where cod = ?";
					PreparedStatement cmd = conn.prepareStatement(query);
					String index;
					index = (String) table.getModel().getValueAt(
							table.getSelectedRow(), 0);

					cmd.setString(1, index);
					cmd.executeUpdate();

				} catch (SQLException ex) {
					System.out.println("Ocorreu um erro sql" + ex.getMessage());
				}

				ArrayList<String> Parametros = new ArrayList<String>();
				Parametros.add(textField.getText().toUpperCase());

				Vector<String> vColunas = new Vector<String>();
				vColunas = PegaColunaDaGrade();

				DefaultTableModel objTbela = new DefaultTableModel(vColunas, 0);

				ControlMesa cntrlMesa = new ControlMesa();

				objTbela = cntrlMesa.Pesquisa(Parametros, objTbela);

				table.setModel(objTbela);

			}

			private Vector<String> PegaColunaDaGrade() {

				Vector<String> colunasTabela = new Vector<String>();
				colunasTabela.add("cod");
				colunasTabela.add("quant_lugar");

				return colunasTabela;
				
			}
		});
		button_5.setForeground(new Color(255, 215, 0));
		button_5.setBackground(new Color(0, 100, 0));
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane
				.setHorizontalGroup(gl_contentPane
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								gl_contentPane
										.createSequentialGroup()
										.addGroup(
												gl_contentPane
														.createParallelGroup(
																Alignment.LEADING)
														.addComponent(
																panel,
																GroupLayout.PREFERRED_SIZE,
																782,
																GroupLayout.PREFERRED_SIZE)
														.addComponent(
																separator,
																GroupLayout.PREFERRED_SIZE,
																781,
																GroupLayout.PREFERRED_SIZE)
														.addComponent(
																separator_1,
																GroupLayout.PREFERRED_SIZE,
																783,
																GroupLayout.PREFERRED_SIZE)
														.addComponent(
																panel_2,
																GroupLayout.PREFERRED_SIZE,
																784,
																GroupLayout.PREFERRED_SIZE)
														.addGroup(
																Alignment.TRAILING,
																gl_contentPane
																		.createSequentialGroup()
																		.addComponent(
																				panel_1,
																				GroupLayout.DEFAULT_SIZE,
																				GroupLayout.DEFAULT_SIZE,
																				Short.MAX_VALUE)
																		.addGap(18)
																		.addGroup(
																				gl_contentPane
																						.createParallelGroup(
																								Alignment.LEADING)
																						.addComponent(
																								button_4,
																								GroupLayout.PREFERRED_SIZE,
																								129,
																								GroupLayout.PREFERRED_SIZE)
																						.addComponent(
																								button_5,
																								GroupLayout.PREFERRED_SIZE,
																								129,
																								GroupLayout.PREFERRED_SIZE))
																		.addGap(69)))
										.addContainerGap(
												GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)));
		gl_contentPane
				.setVerticalGroup(gl_contentPane
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								gl_contentPane
										.createSequentialGroup()
										.addComponent(panel,
												GroupLayout.PREFERRED_SIZE, 65,
												GroupLayout.PREFERRED_SIZE)
										.addGap(18)
										.addComponent(separator,
												GroupLayout.PREFERRED_SIZE, 5,
												GroupLayout.PREFERRED_SIZE)
										.addGroup(
												gl_contentPane
														.createParallelGroup(
																Alignment.LEADING)
														.addGroup(
																gl_contentPane
																		.createSequentialGroup()
																		.addGap(93)
																		.addComponent(
																				button_4,
																				GroupLayout.PREFERRED_SIZE,
																				48,
																				GroupLayout.PREFERRED_SIZE)
																		.addPreferredGap(
																				ComponentPlacement.UNRELATED)
																		.addComponent(
																				button_5,
																				GroupLayout.PREFERRED_SIZE,
																				48,
																				GroupLayout.PREFERRED_SIZE))
														.addGroup(
																gl_contentPane
																		.createSequentialGroup()
																		.addGap(13)
																		.addComponent(
																				panel_1,
																				GroupLayout.PREFERRED_SIZE,
																				310,
																				GroupLayout.PREFERRED_SIZE)))
										.addGap(56)
										.addComponent(separator_1,
												GroupLayout.PREFERRED_SIZE, 8,
												GroupLayout.PREFERRED_SIZE)
										.addGap(12)
										.addComponent(panel_2,
												GroupLayout.PREFERRED_SIZE, 75,
												GroupLayout.PREFERRED_SIZE)
										.addContainerGap(
												GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)));
		contentPane.setLayout(gl_contentPane);
	}
}
